/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：呼叫中心-数据源-数据持久层-权限控制管理
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taiping.cc.authorize.dao;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.taiping.cc.application.constant.dao.ActiveEnum;
import com.taiping.cc.application.constant.dao.BatchSizeEnum;
import com.taiping.cc.application.constant.dao.LoginStatusEnum;
import com.taiping.cc.authorize.dao.mapper.AuthLoginMapper;
import com.taiping.cc.authorize.dataobject.AuthLoginDO;
import com.taipingframework.mybatis.CommonDAO;
import com.taipingframework.utility.exception.DaoException;
import com.taipingframework.utility.thread.ForkJoinHelper;
import com.taipingframework.utility.thread.wrapper.BenchmarksFuture;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 网络服务请求及响应日志记录表 服务实现类
 * </p>
 *
 * @author 周孝兵
 * @since 2021-11-29
 */
@Repository
public class AuthLoginDAO extends CommonDAO<AuthLoginMapper, AuthLoginDO> {

    /**
     * 根据登录名查询用户数据
     */
    public List<AuthLoginDO> queryWithLoginName(String loginName) throws DaoException {
        try {
            LambdaQueryWrapper<AuthLoginDO> queryWrapper = Wrappers.<AuthLoginDO>lambdaQuery()
                    .eq(AuthLoginDO::getActive, ActiveEnum.ENABLE.getCode())
                    .eq(AuthLoginDO::getLoginStatus, LoginStatusEnum.ENABLE.getCode())
                    .eq(AuthLoginDO::getLoginName, loginName);
            List<AuthLoginDO> list = this.list(queryWrapper);
            if (list == null) {
                return Lists.newArrayList();
            }
            return list;
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    /**
     * 批量插入用户登录认证数据
     */
    public BenchmarksFuture<Boolean> insertAuthLoginDataBatch(List<AuthLoginDO> authLoginDOList) {
        return BenchmarksFuture.supplyAsync(() -> {
            if (authLoginDOList == null || authLoginDOList.size() <= 0) {
                return false;
            }

            // 如果业务ID为空，此处需要生成业务ID
            ForkJoinHelper.execute(() -> authLoginDOList.parallelStream()
                    .forEach(authLoginDO -> {
                        if (StringUtils.isBlank(authLoginDO.getLoginId())) {
                            authLoginDO.setLoginId(IdUtil.objectId());
                        }
                    }));

            return this.saveOrUpdateBatch(authLoginDOList, BatchSizeEnum.INSERT_SIZE_100.getSize());
        }).exceptionally(throwable -> {
            throw new DaoException(throwable.getCause());
        });
    }

    /**
     * 禁用账户
     */
    public Integer disableAccount(String loginName) throws DaoException {
        try {
            LambdaQueryWrapper<AuthLoginDO> queryWrapper = Wrappers.<AuthLoginDO>lambdaQuery()
                    .eq(AuthLoginDO::getLoginName, loginName);
            return this.delete(queryWrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

}
